home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / wgdb-42.lha / wgdb-4.2 / gdb / values.c < prev    next >
C/C++ Source or Header  |  1992-09-11  |  46KB  |  1,606 lines

  1. /* Low level packing and unpacking of values for GDB.
  2.    Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include "defs.h"
  23. #include "param.h"
  24. #include "symtab.h"
  25. #include "value.h"
  26. #include "gdbcore.h"
  27. #include "frame.h"
  28. #include "command.h"
  29. #include "gdbcmd.h"
  30.  
  31. extern char *cplus_demangle ();
  32. extern char *cplus_mangle_opname ();
  33.  
  34. /* The value-history records all the values printed
  35.    by print commands during this session.  Each chunk
  36.    records 60 consecutive values.  The first chunk on
  37.    the chain records the most recent values.
  38.    The total number of values is in value_history_count.  */
  39.  
  40. #define VALUE_HISTORY_CHUNK 60
  41.  
  42. struct value_history_chunk
  43. {
  44.   struct value_history_chunk *next;
  45.   value values[VALUE_HISTORY_CHUNK];
  46. };
  47.  
  48. /* Chain of chunks now in use.  */
  49.  
  50. static struct value_history_chunk *value_history_chain;
  51.  
  52. static int value_history_count;    /* Abs number of last entry stored */
  53.  
  54. /* List of all value objects currently allocated
  55.    (except for those released by calls to release_value)
  56.    This is so they can be freed after each command.  */
  57.  
  58. static value all_values;
  59.  
  60. /* Allocate a  value  that has the correct length for type TYPE.  */
  61.  
  62. value
  63. allocate_value (type)
  64.      struct type *type;
  65. {
  66.   register value val;
  67.  
  68.   check_stub_type (type);
  69.  
  70.   val = (value) xmalloc (sizeof (struct value) + TYPE_LENGTH (type));
  71.   VALUE_NEXT (val) = all_values;
  72.   all_values = val;
  73.   VALUE_TYPE (val) = type;
  74.   VALUE_LVAL (val) = not_lval;
  75.   VALUE_ADDRESS (val) = 0;
  76.   VALUE_FRAME (val) = 0;
  77.   VALUE_OFFSET (val) = 0;
  78.   VALUE_BITPOS (val) = 0;
  79.   VALUE_BITSIZE (val) = 0;
  80.   VALUE_REPEATED (val) = 0;
  81.   VALUE_REPETITIONS (val) = 0;
  82.   VALUE_REGNO (val) = -1;
  83.   VALUE_LAZY (val) = 0;
  84.   VALUE_OPTIMIZED_OUT (val) = 0;
  85.   return val;
  86. }
  87.  
  88. /* Allocate a  value  that has the correct length
  89.    for COUNT repetitions type TYPE.  */
  90.  
  91. value
  92. allocate_repeat_value (type, count)
  93.      struct type *type;
  94.      int count;
  95. {
  96.   register value val;
  97.  
  98.   val = (value) xmalloc (sizeof (struct value) + TYPE_LENGTH (type) * count);
  99.   VALUE_NEXT (val) = all_values;
  100.   all_values = val;
  101.   VALUE_TYPE (val) = type;
  102.   VALUE_LVAL (val) = not_lval;
  103.   VALUE_ADDRESS (val) = 0;
  104.   VALUE_FRAME (val) = 0;
  105.   VALUE_OFFSET (val) = 0;
  106.   VALUE_BITPOS (val) = 0;
  107.   VALUE_BITSIZE (val) = 0;
  108.   VALUE_REPEATED (val) = 1;
  109.   VALUE_REPETITIONS (val) = count;
  110.   VALUE_REGNO (val) = -1;
  111.   VALUE_LAZY (val) = 0;
  112.   VALUE_OPTIMIZED_OUT (val) = 0;
  113.   return val;
  114. }
  115.  
  116. /* Return a mark in the value chain.  All values allocated after the
  117.    mark is obtained (except for those released) are subject to being freed
  118.    if a subsequent value_free_to_mark is passed the mark.  */
  119. value
  120. value_mark ()
  121. {
  122.   return all_values;
  123. }
  124.  
  125. /* Free all values allocated since MARK was obtained by value_mark
  126.    (except for those released).  */
  127. void
  128. value_free_to_mark (mark)
  129.      value mark;
  130. {
  131.   value val, next;
  132.  
  133.   for (val = all_values; val && val != mark; val = next)
  134.     {
  135.       next = VALUE_NEXT (val);
  136.       value_free (val);
  137.     }
  138.   all_values = val;
  139. }
  140.  
  141. /* Free all the values that have been allocated (except for those released).
  142.    Called after each command, successful or not.  */
  143.  
  144. void
  145. free_all_values ()
  146. {
  147.   register value val, next;
  148.  
  149.   for (val = all_values; val; val = next)
  150.     {
  151.       next = VALUE_NEXT (val);
  152.       value_free (val);
  153.     }
  154.  
  155.   all_values = 0;
  156. }
  157.  
  158. /* Remove VAL from the chain all_values
  159.    so it will not be freed automatically.  */
  160.  
  161. void
  162. release_value (val)
  163.      register value val;
  164. {
  165.   register value v;
  166.  
  167.   if (all_values == val)
  168.     {
  169.       all_values = val->next;
  170.       return;
  171.     }
  172.  
  173.   for (v = all_values; v; v = v->next)
  174.     {
  175.       if (v->next == val)
  176.     {
  177.       v->next = val->next;
  178.       break;
  179.     }
  180.     }
  181. }
  182.  
  183. /* Return a copy of the value ARG.
  184.    It contains the same contents, for same memory address,
  185.    but it's a different block of storage.  */
  186.  
  187. static value
  188. value_copy (arg)
  189.      value arg;
  190. {
  191.   register value val;
  192.   register struct type *type = VALUE_TYPE (arg);
  193.   if (VALUE_REPEATED (arg))
  194.     val = allocate_repeat_value (type, VALUE_REPETITIONS (arg));
  195.   else
  196.     val = allocate_value (type);
  197.   VALUE_LVAL (val) = VALUE_LVAL (arg);
  198.   VALUE_ADDRESS (val) = VALUE_ADDRESS (arg);
  199.   VALUE_OFFSET (val) = VALUE_OFFSET (arg);
  200.   VALUE_BITPOS (val) = VALUE_BITPOS (arg);
  201.   VALUE_BITSIZE (val) = VALUE_BITSIZE (arg);
  202.   VALUE_REGNO (val) = VALUE_REGNO (arg);
  203.   VALUE_LAZY (val) = VALUE_LAZY (arg);
  204.   if (!VALUE_LAZY (val))
  205.     {
  206.       bcopy (VALUE_CONTENTS_RAW (arg), VALUE_CONTENTS_RAW (val),
  207.          TYPE_LENGTH (VALUE_TYPE (arg))
  208.          * (VALUE_REPEATED (arg) ? VALUE_REPETITIONS (arg) : 1));
  209.     }
  210.   return val;
  211. }
  212.  
  213. /* Access to the value history.  */
  214.  
  215. /* Record a new value in the value history.
  216.    Returns the absolute history index of the entry.
  217.    Result of -1 indicates the value was not saved; otherwise it is the
  218.    value history index of this new item.  */
  219.  
  220. int
  221. record_latest_value (val)
  222.      value val;
  223. {
  224.   int i;
  225.  
  226.   /* Check error now if about to store an invalid float.  We return -1
  227.      to the caller, but allow them to continue, e.g. to print it as "Nan". */
  228.   if (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_FLT) {
  229.     (void) unpack_double (VALUE_TYPE (val), VALUE_CONTENTS (val), &i);
  230.     if (i) return -1;        /* Indicate value not saved in history */
  231.   }
  232.  
  233.   /* Here we treat value_history_count as origin-zero
  234.      and applying to the value being stored now.  */
  235.  
  236.   i = value_history_count % VALUE_HISTORY_CHUNK;
  237.   if (i == 0)
  238.     {
  239.       register struct value_history_chunk *new
  240.     = (struct value_history_chunk *)
  241.       xmalloc (sizeof (struct value_history_chunk));
  242.       bzero (new->values, sizeof new->values);
  243.       new->next = value_history_chain;
  244.       value_history_chain = new;
  245.     }
  246.  
  247.   value_history_chain->values[i] = val;
  248.   release_value (val);
  249.  
  250.   /* Now we regard value_history_count as origin-one
  251.      and applying to the value just stored.  */
  252.  
  253.   return ++value_history_count;
  254. }
  255.  
  256. /* Return a copy of the value in the history with sequence number NUM.  */
  257.  
  258. value
  259. access_value_history (num)
  260.      int num;
  261. {
  262.   register struct value_history_chunk *chunk;
  263.   register int i;
  264.   register int absnum = num;
  265.  
  266.   if (absnum <= 0)
  267.     absnum += value_history_count;
  268.  
  269.   if (absnum <= 0)
  270.     {
  271.       if (num == 0)
  272.     error ("The history is empty.");
  273.       else if (num == 1)
  274.     error ("There is only one value in the history.");
  275.       else
  276.     error ("History does not go back to $$%d.", -num);
  277.     }
  278.   if (absnum > value_history_count)
  279.     error ("History has not yet reached $%d.", absnum);
  280.  
  281.   absnum--;
  282.  
  283.   /* Now absnum is always absolute and origin zero.  */
  284.  
  285.   chunk = value_history_chain;
  286.   for (i = (value_history_count - 1) / VALUE_HISTORY_CHUNK - absnum / VALUE_HISTORY_CHUNK;
  287.        i > 0; i--)
  288.     chunk = chunk->next;
  289.  
  290.   return value_copy (chunk->values[absnum % VALUE_HISTORY_CHUNK]);
  291. }
  292.  
  293. /* Clear the value history entirely.
  294.    Must be done when new symbol tables are loaded,
  295.    because the type pointers become invalid.  */
  296.  
  297. void
  298. clear_value_history ()
  299. {
  300.   register struct value_history_chunk *next;
  301.   register int i;
  302.   register value val;
  303.  
  304.   while (value_history_chain)
  305.     {
  306.       for (i = 0; i < VALUE_HISTORY_CHUNK; i++)
  307.     if (val = value_history_chain->values[i])
  308.       free (val);
  309.       next = value_history_chain->next;
  310.       free (value_history_chain);
  311.       value_history_chain = next;
  312.     }
  313.   value_history_count = 0;
  314. }
  315.  
  316. static void
  317. show_values (num_exp, from_tty)
  318.      char *num_exp;
  319.      int from_tty;
  320. {
  321.   register int i;
  322.   register value val;
  323.   static int num = 1;
  324.  
  325.   if (num_exp)
  326.     {
  327.       if (num_exp[0] == '+' && num_exp[1] == '\0')
  328.     /* "info history +" should print from the stored position.  */
  329.     ;
  330.       else
  331.     /* "info history <exp>" should print around value number <exp>.  */
  332.     num = parse_and_eval_address (num_exp) - 5;
  333.     }
  334.   else
  335.     {
  336.       /* "info history" means print the last 10 values.  */
  337.       num = value_history_count - 9;
  338.     }
  339.  
  340.   if (num <= 0)
  341.     num = 1;
  342.  
  343.   for (i = num; i < num + 10 && i <= value_history_count; i++)
  344.     {
  345.       val = access_value_history (i);
  346.       printf_filtered ("$%d = ", i);
  347.       value_print (val, stdout, 0, Val_pretty_default);
  348.       printf_filtered ("\n");
  349.     }
  350.  
  351.   /* The next "info history +" should start after what we just printed.  */
  352.   num += 10;
  353.  
  354.   /* Hitting just return after this command should do the same thing as
  355.      "info history +".  If num_exp is null, this is unnecessary, since
  356.      "info history +" is not useful after "info history".  */
  357.   if (from_tty && num_exp)
  358.     {
  359.       num_exp[0] = '+';
  360.       num_exp[1] = '\0';
  361.     }
  362. }
  363.  
  364. /* Internal variables.  These are variables within the debugger
  365.    that hold values assigned by debugger commands.
  366.    The user refers to them with a '$' prefix
  367.    that does not appear in the variable names stored internally.  */
  368.  
  369. static struct internalvar *internalvars;
  370.  
  371. /* Look up an internal variable with name NAME.  NAME should not
  372.    normally include a dollar sign.
  373.  
  374.    If the specified internal variable does not exist,
  375.    one is created, with a void value.  */
  376.  
  377. struct internalvar *
  378. lookup_internalvar (name)
  379.      char *name;
  380. {
  381.   register struct internalvar *var;
  382.  
  383.   for (var = internalvars; var; var = var->next)
  384.     if (!strcmp (var->name, name))
  385.       return var;
  386.  
  387.   var = (struct internalvar *) xmalloc (sizeof (struct internalvar));
  388.   var->name = concat (name, "", "");
  389.   var->value = allocate_value (builtin_type_void);
  390.   release_value (var->value);
  391.   var->next = internalvars;
  392.   internalvars = var;
  393.   return var;
  394. }
  395.  
  396. value
  397. value_of_internalvar (var)
  398.      struct internalvar *var;
  399. {
  400.   register value val;
  401.  
  402. #ifdef IS_TRAPPED_INTERNALVAR
  403.   if (IS_TRAPPED_INTERNALVAR (var->name))
  404.     return VALUE_OF_TRAPPED_INTERNALVAR (var);
  405. #endif 
  406.  
  407.   val = value_copy (var->value);
  408.   if (VALUE_LAZY (val))
  409.     value_fetch_lazy (val);
  410.   VALUE_LVAL (val) = lval_internalvar;
  411.   VALUE_INTERNALVAR (val) = var;
  412.   return val;
  413. }
  414.  
  415. void
  416. set_internalvar_component (var, offset, bitpos, bitsize, newval)
  417.      struct internalvar *var;
  418.      int offset, bitpos, bitsize;
  419.      value newval;
  420. {
  421.   register char *addr = VALUE_CONTENTS (var->value) + offset;
  422.  
  423. #ifdef IS_TRAPPED_INTERNALVAR
  424.   if (IS_TRAPPED_INTERNALVAR (var->name))
  425.     SET_TRAPPED_INTERNALVAR (var, newval, bitpos, bitsize, offset);
  426. #endif
  427.  
  428.   if (bitsize)
  429.     modify_field (addr, (int) value_as_long (newval),
  430.           bitpos, bitsize);
  431.   else
  432.     bcopy (VALUE_CONTENTS (newval), addr,
  433.        TYPE_LENGTH (VALUE_TYPE (newval)));
  434. }
  435.  
  436. void
  437. set_internalvar (var, val)
  438.      struct internalvar *var;
  439.      value val;
  440. {
  441. #ifdef IS_TRAPPED_INTERNALVAR
  442.   if (IS_TRAPPED_INTERNALVAR (var->name))
  443.     SET_TRAPPED_INTERNALVAR (var, val, 0, 0, 0);
  444. #endif
  445.  
  446.   free (var->value);
  447.   var->value = value_copy (val);
  448.   release_value (var->value);
  449. }
  450.  
  451. char *
  452. internalvar_name (var)
  453.      struct internalvar *var;
  454. {
  455.   return var->name;
  456. }
  457.  
  458. /* Free all internalvars.  Done when new symtabs are loaded,
  459.    because that makes the values invalid.  */
  460.  
  461. void
  462. clear_internalvars ()
  463. {
  464.   register struct internalvar *var;
  465.  
  466.   while (internalvars)
  467.     {
  468.       var = internalvars;
  469.       internalvars = var->next;
  470.       free (var->name);
  471.       free (var->value);
  472.       free (var);
  473.     }
  474. }
  475.  
  476. static void
  477. show_convenience ()
  478. {
  479.   register struct internalvar *var;
  480.   int varseen = 0;
  481.  
  482.   for (var = internalvars; var; var = var->next)
  483.     {
  484. #ifdef IS_TRAPPED_INTERNALVAR
  485.       if (IS_TRAPPED_INTERNALVAR (var->name))
  486.     continue;
  487. #endif
  488.       if (!varseen)
  489.     {
  490. #if 0
  491.       /* Useless noise.  */
  492.       printf ("Debugger convenience variables:\n\n");
  493. #endif
  494.       varseen = 1;
  495.     }
  496.       printf_filtered ("$%s = ", var->name);
  497.       value_print (var->value, stdout, 0, Val_pretty_default);
  498.       printf_filtered ("\n");
  499.     }
  500.   if (!varseen)
  501.     printf ("No debugger convenience variables now defined.\n\
  502. Convenience variables have names starting with \"$\";\n\
  503. use \"set\" as in \"set $foo = 5\" to define them.\n");
  504. }
  505.  
  506. /* Extract a value as a C number (either long or double).
  507.    Knows how to convert fixed values to double, or
  508.    floating values to long.
  509.    Does not deallocate the value.  */
  510.  
  511. LONGEST
  512. value_as_long (val)
  513.      register value val;
  514. {
  515.   /* This coerces arrays and functions, which is necessary (e.g.
  516.      in disassemble_command).  It also dereferences references, which
  517.      I suspect is the most logical thing to do.  */
  518.   if (TYPE_CODE (VALUE_TYPE (val)) != TYPE_CODE_ENUM)
  519.     COERCE_ARRAY (val);
  520.   return unpack_long (VALUE_TYPE (val), VALUE_CONTENTS (val));
  521. }
  522.  
  523. double
  524. value_as_double (val)
  525.      register value val;
  526. {
  527.   double foo;
  528.   int inv;
  529.   
  530.   foo = unpack_double (VALUE_TYPE (val), VALUE_CONTENTS (val), &inv);
  531.   if (inv)
  532.     error ("Invalid floating value found in program.");
  533.   return foo;
  534. }
  535. /* Extract a value as a C pointer.
  536.    Does not deallocate the value.  */
  537. CORE_ADDR
  538. value_as_pointer (val)
  539.      value val;
  540. {
  541.   /* Assume a CORE_ADDR can fit in a LONGEST (for now).  Not sure
  542.      whether we want this to be true eventually.  */
  543.   return value_as_long (val);
  544. }
  545.  
  546. /* Unpack raw data (copied from debugee, target byte order) at VALADDR
  547.    as a long, or as a double, assuming the raw data is described
  548.    by type TYPE.  Knows how to convert different sizes of values
  549.    and can convert between fixed and floating point.  We don't assume
  550.    any alignment for the raw data.  Return value is in host byte order.
  551.  
  552.    If you want functions and arrays to be coerced to pointers, and
  553.    references to be dereferenced, call value_as_long() instead.
  554.  
  555.    C++: It is assumed that the front-end has taken care of
  556.    all matters concerning pointers to members.  A pointer
  557.    to member which reaches here is considered to be equivalent
  558.    to an INT (or some size).  After all, it is only an offset.  */
  559.  
  560. /* FIXME:  This should be rewritten as a switch statement for speed and
  561.    ease of comprehension.  */
  562.  
  563. LONGEST
  564. unpack_long (type, valaddr)
  565.      struct type *type;
  566.      char *valaddr;
  567. {
  568.   register enum type_code code = TYPE_CODE (type);
  569.   register int len = TYPE_LENGTH (type);
  570.   register int nosign = TYPE_UNSIGNED (type);
  571.  
  572.   if (code == TYPE_CODE_ENUM || code == TYPE_CODE_BOOL)
  573.     code = TYPE_CODE_INT;
  574.   if (code == TYPE_CODE_FLT)
  575.     {
  576.       if (len == sizeof (float))
  577.     {
  578.       float retval;
  579.       bcopy (valaddr, &retval, sizeof (retval));
  580.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  581.       return retval;
  582.     }
  583.  
  584.       if (len == sizeof (double))
  585.     {
  586.       double retval;
  587.       bcopy (valaddr, &retval, sizeof (retval));
  588.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  589.       return retval;
  590.     }
  591.       else
  592.     {
  593.       error ("Unexpected type of floating point number.");
  594.     }
  595.     }
  596.   else if (code == TYPE_CODE_INT && nosign)
  597.     {
  598.       if (len == sizeof (char))
  599.     {
  600.       unsigned char retval = * (unsigned char *) valaddr;
  601.       /* SWAP_TARGET_AND_HOST (&retval, sizeof (unsigned char)); */
  602.       return retval;
  603.     }
  604.  
  605.       if (len == sizeof (short))
  606.     {
  607.       unsigned short retval;
  608.       bcopy (valaddr, &retval, sizeof (retval));
  609.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  610.       return retval;
  611.     }
  612.  
  613.       if (len == sizeof (int))
  614.     {
  615.       unsigned int retval;
  616.       bcopy (valaddr, &retval, sizeof (retval));
  617.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  618.       return retval;
  619.     }
  620.  
  621.       if (len == sizeof (long))
  622.     {
  623.       unsigned long retval;
  624.       bcopy (valaddr, &retval, sizeof (retval));
  625.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  626.       return retval;
  627.     }
  628. #ifdef LONG_LONG
  629.       if (len == sizeof (long long))
  630.     {
  631.       unsigned long long retval;
  632.       bcopy (valaddr, &retval, sizeof (retval));
  633.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  634.       return retval;
  635.     }
  636. #endif
  637.       else
  638.     {
  639.       error ("That operation is not possible on an integer of that size.");
  640.     }
  641.     }
  642.   else if (code == TYPE_CODE_INT)
  643.     {
  644.       if (len == sizeof (char))
  645.     {
  646.       char retval;
  647.       bcopy (valaddr, &retval, sizeof (retval));
  648.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  649.       return retval;
  650.     }
  651.  
  652.       if (len == sizeof (short))
  653.     {
  654.       short retval;
  655.       bcopy (valaddr, &retval, sizeof (retval));
  656.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  657.       return retval;
  658.     }
  659.  
  660.       if (len == sizeof (int))
  661.     {
  662.       int retval;
  663.       bcopy (valaddr, &retval, sizeof (retval));
  664.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  665.       return retval;
  666.     }
  667.  
  668.       if (len == sizeof (long))
  669.     {
  670.       long retval;
  671.       bcopy (valaddr, &retval, sizeof (retval));
  672.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  673.       return retval;
  674.     }
  675.  
  676. #ifdef LONG_LONG
  677.       if (len == sizeof (long long))
  678.     {
  679.       long long retval;
  680.       bcopy (valaddr, &retval, sizeof (retval));
  681.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  682.       return retval;
  683.     }
  684. #endif
  685.       else
  686.     {
  687.       error ("That operation is not possible on an integer of that size.");
  688.     }
  689.     }
  690.   /* Assume a CORE_ADDR can fit in a LONGEST (for now).  Not sure
  691.      whether we want this to be true eventually.  */
  692.   else if (code == TYPE_CODE_PTR
  693.        || code == TYPE_CODE_REF)
  694.     {
  695.       if (len == sizeof (CORE_ADDR))
  696.     {
  697.       CORE_ADDR retval;
  698.       bcopy (valaddr, &retval, sizeof (retval));
  699.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  700.       return retval;
  701.     }
  702.     }
  703.   else if (code == TYPE_CODE_MEMBER)
  704.     error ("not implemented: member types in unpack_long");
  705.   else if (code == TYPE_CODE_CHAR)
  706.     return *(unsigned char *)valaddr;
  707.  
  708.   error ("Value not integer or pointer.");
  709.   return 0;     /* For lint -- never reached */
  710. }
  711.  
  712. /* Return a double value from the specified type and address.
  713.    INVP points to an int which is set to 0 for valid value,
  714.    1 for invalid value (bad float format).  In either case,
  715.    the returned double is OK to use.  Argument is in target
  716.    format, result is in host format.  */
  717.  
  718. double
  719. unpack_double (type, valaddr, invp)
  720.      struct type *type;
  721.      char *valaddr;
  722.      int *invp;
  723. {
  724.   register enum type_code code = TYPE_CODE (type);
  725.   register int len = TYPE_LENGTH (type);
  726.   register int nosign = TYPE_UNSIGNED (type);
  727.  
  728.   *invp = 0;            /* Assume valid.   */
  729.   if (code == TYPE_CODE_FLT)
  730.     {
  731.       if (INVALID_FLOAT (valaddr, len))
  732.     {
  733.       *invp = 1;
  734.       return 1.234567891011121314;
  735.     }
  736.  
  737.       if (len == sizeof (float))
  738.     {
  739.       float retval;
  740.       bcopy (valaddr, &retval, sizeof (retval));
  741.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  742.       return retval;
  743.     }
  744.  
  745.       if (len == sizeof (double))
  746.     {
  747.       double retval;
  748.       bcopy (valaddr, &retval, sizeof (retval));
  749.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  750.       return retval;
  751.     }
  752.       else
  753.     {
  754.       error ("Unexpected type of floating point number.");
  755.       return 0; /* Placate lint.  */
  756.     }
  757.     }
  758.   else if (nosign) {
  759.    /* Unsigned -- be sure we compensate for signed LONGEST.  */
  760. #ifdef LONG_LONG
  761.    return (unsigned long long) unpack_long (type, valaddr);
  762. #else
  763.    return (unsigned long     ) unpack_long (type, valaddr);
  764. #endif
  765.   } else {
  766.     /* Signed -- we are OK with unpack_long.  */
  767.     return unpack_long (type, valaddr);
  768.   }
  769. }
  770.  
  771. /* Unpack raw data (copied from debugee, target byte order) at VALADDR
  772.    as a CORE_ADDR, assuming the raw data is described by type TYPE.
  773.    We don't assume any alignment for the raw data.  Return value is in
  774.    host byte order.
  775.  
  776.    If you want functions and arrays to be coerced to pointers, and
  777.    references to be dereferenced, call value_as_pointer() instead.
  778.  
  779.    C++: It is assumed that the front-end has taken care of
  780.    all matters concerning pointers to members.  A pointer
  781.    to member which reaches here is considered to be equivalent
  782.    to an INT (or some size).  After all, it is only an offset.  */
  783.  
  784. CORE_ADDR
  785. unpack_pointer (type, valaddr)
  786.      struct type *type;
  787.      char *valaddr;
  788. {
  789. #if 0
  790.   /* The user should be able to use an int (e.g. 0x7892) in contexts
  791.      where a pointer is expected.  So this doesn't do enough.  */
  792.   register enum type_code code = TYPE_CODE (type);
  793.   register int len = TYPE_LENGTH (type);
  794.  
  795.   if (code == TYPE_CODE_PTR
  796.       || code == TYPE_CODE_REF)
  797.     {
  798.       if (len == sizeof (CORE_ADDR))
  799.     {
  800.       CORE_ADDR retval;
  801.       bcopy (valaddr, &retval, sizeof (retval));
  802.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  803.       return retval;
  804.     }
  805.       error ("Unrecognized pointer size.");
  806.     }
  807.   else if (code == TYPE_CODE_MEMBER)
  808.     error ("not implemented: member types in unpack_pointer");
  809.  
  810.   error ("Value is not a pointer.");
  811.   return 0;     /* For lint -- never reached */
  812. #else
  813.   /* Assume a CORE_ADDR can fit in a LONGEST (for now).  Not sure
  814.      whether we want this to be true eventually.  */
  815.   return unpack_long (type, valaddr);
  816. #endif
  817. }
  818.  
  819. /* Given a value ARG1 (offset by OFFSET bytes)
  820.    of a struct or union type ARG_TYPE,
  821.    extract and return the value of one of its fields.
  822.    FIELDNO says which field.
  823.  
  824.    For C++, must also be able to return values from static fields */
  825.  
  826. value
  827. value_primitive_field (arg1, offset, fieldno, arg_type)
  828.      register value arg1;
  829.      int offset;
  830.      register int fieldno;
  831.      register struct type *arg_type;
  832. {
  833.   register value v;
  834.   register struct type *type;
  835.  
  836.   check_stub_type (arg_type);
  837.   type = TYPE_FIELD_TYPE (arg_type, fieldno);
  838.  
  839.   /* Handle packed fields */
  840.  
  841.   offset += TYPE_FIELD_BITPOS (arg_type, fieldno) / 8;
  842.   if (TYPE_FIELD_BITSIZE (arg_type, fieldno))
  843.     {
  844.       v = value_from_longest (type,
  845.                unpack_field_as_long (arg_type,
  846.                          VALUE_CONTENTS (arg1),
  847.                          fieldno));
  848.       VALUE_BITPOS (v) = TYPE_FIELD_BITPOS (arg_type, fieldno) % 8;
  849.       VALUE_BITSIZE (v) = TYPE_FIELD_BITSIZE (arg_type, fieldno);
  850.     }
  851.   else
  852.     {
  853.       v = allocate_value (type);
  854.       if (VALUE_LAZY (arg1))
  855.     VALUE_LAZY (v) = 1;
  856.       else
  857.     bcopy (VALUE_CONTENTS_RAW (arg1) + offset,
  858.            VALUE_CONTENTS_RAW (v),
  859.            TYPE_LENGTH (type));
  860.     }
  861.   VALUE_LVAL (v) = VALUE_LVAL (arg1);
  862.   if (VALUE_LVAL (arg1) == lval_internalvar)
  863.     VALUE_LVAL (v) = lval_internalvar_component;
  864.   VALUE_ADDRESS (v) = VALUE_ADDRESS (arg1);
  865.   VALUE_OFFSET (v) = offset + VALUE_OFFSET (arg1);
  866.   return v;
  867. }
  868.  
  869. /* Given a value ARG1 of a struct or union type,
  870.    extract and return the value of one of its fields.
  871.    FIELDNO says which field.
  872.  
  873.    For C++, must also be able to return values from static fields */
  874.  
  875. value
  876. value_field (arg1, fieldno)
  877.      register value arg1;
  878.      register int fieldno;
  879. {
  880.   return value_primitive_field (arg1, 0, fieldno, VALUE_TYPE (arg1));
  881. }
  882.  
  883. value
  884. value_fn_field (arg1, fieldno, subfieldno)
  885.      register value arg1;
  886.      register int fieldno;
  887.      int subfieldno;
  888. {
  889.   register value v;
  890.   struct fn_field *f = TYPE_FN_FIELDLIST1 (VALUE_TYPE (arg1), fieldno);
  891.   register struct type *type = TYPE_FN_FIELD_TYPE (f, subfieldno);
  892.   struct symbol *sym;
  893.  
  894.   sym = lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, subfieldno),
  895.                0, VAR_NAMESPACE, 0, NULL);
  896.   if (! sym) error ("Internal error: could not find physical method named %s",
  897.             TYPE_FN_FIELD_PHYSNAME (f, subfieldno));
  898.   
  899.   v = allocate_value (type);
  900.   VALUE_ADDRESS (v) = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
  901.   VALUE_TYPE (v) = type;
  902.   return v;
  903. }
  904.  
  905. /* Return a virtual function as a value.
  906.    ARG1 is the object which provides the virtual function
  907.    table pointer.  ARG1 is side-effected in calling this function.
  908.    F is the list of member functions which contains the desired virtual
  909.    function.
  910.    J is an index into F which provides the desired virtual function.
  911.  
  912.    TYPE is the type in which F is located.  */
  913. value
  914. value_virtual_fn_field (arg1, f, j, type)
  915.      value arg1;
  916.      struct fn_field *f;
  917.      int j;
  918.      struct type *type;
  919. {
  920.   /* First, get the virtual function table pointer.  That comes
  921.      with a strange type, so cast it to type `pointer to long' (which
  922.      should serve just fine as a function type).  Then, index into
  923.      the table, and convert final value to appropriate function type.  */
  924.   value entry, vfn, vtbl;
  925.   value vi = value_from_longest (builtin_type_int, 
  926.                   (LONGEST) TYPE_FN_FIELD_VOFFSET (f, j));
  927.   struct type *fcontext = TYPE_FN_FIELD_FCONTEXT (f, j);
  928.   struct type *context;
  929.   if (fcontext == NULL)
  930.    /* We don't have an fcontext (e.g. the program was compiled with
  931.       g++ version 1).  Try to get the vtbl from the TYPE_VPTR_BASETYPE.
  932.       This won't work right for multiple inheritance, but at least we
  933.       should do as well as GDB 3.x did.  */
  934.     fcontext = TYPE_VPTR_BASETYPE (type);
  935.   context = lookup_pointer_type (fcontext);
  936.   /* Now context is a pointer to the basetype containing the vtbl.  */
  937.   if (TYPE_TARGET_TYPE (context) != VALUE_TYPE (arg1))
  938.     arg1 = value_ind (value_cast (context, value_addr (arg1)));
  939.  
  940.   context = VALUE_TYPE (arg1);
  941.   /* Now context is the basetype containing the vtbl.  */
  942.  
  943.   /* This type may have been defined before its virtual function table
  944.      was.  If so, fill in the virtual function table entry for the
  945.      type now.  */
  946.   if (TYPE_VPTR_FIELDNO (context) < 0)
  947.     fill_in_vptr_fieldno (context);
  948.  
  949.   /* The virtual function table is now an array of structures
  950.      which have the form { int16 offset, delta; void *pfn; }.  */
  951.   vtbl = value_ind (value_field (arg1, TYPE_VPTR_FIELDNO (context)));
  952.  
  953.   /* Index into the virtual function table.  This is hard-coded because
  954.      looking up a field is not cheap, and it may be important to save
  955.      time, e.g. if the user has set a conditional breakpoint calling
  956.      a virtual function.  */
  957.   entry = value_subscript (vtbl, vi);
  958.  
  959.   /* Move the `this' pointer according to the virtual function table.  */
  960.   VALUE_OFFSET (arg1) += value_as_long (value_field (entry, 0));
  961.   if (! VALUE_LAZY (arg1))
  962.     {
  963.       VALUE_LAZY (arg1) = 1;
  964.       value_fetch_lazy (arg1);
  965.     }
  966.  
  967.   vfn = value_field (entry, 2);
  968.   /* Reinstantiate the function pointer with the correct type.  */
  969.   VALUE_TYPE (vfn) = lookup_pointer_type (TYPE_FN_FIELD_TYPE (f, j));
  970.  
  971.   return vfn;
  972. }
  973.  
  974. /* ARG is a pointer to an object we know to be at least
  975.    a DTYPE.  BTYPE is the most derived basetype that has
  976.    already been searched (and need not be searched again).
  977.    After looking at the vtables between BTYPE and DTYPE,
  978.    return the most derived type we find.  The caller must
  979.    be satisfied when the return value == DTYPE.
  980.  
  981.    FIXME-tiemann: should work with dossier entries as well.  */
  982.  
  983. static value
  984. value_headof (arg, btype, dtype)
  985.      value arg;
  986.      struct type *btype, *dtype;
  987. {
  988.   /* First collect the vtables we must look at for this object.  */
  989.   /* FIXME-tiemann: right now, just look at top-most vtable.  */
  990.   value vtbl, entry, best_entry = 0;
  991.   /* FIXME: entry_type is never used.  */
  992.   struct type *entry_type;
  993.   int i, nelems;
  994.   int offset, best_offset = 0;
  995.   struct symbol *sym;
  996.   CORE_ADDR pc_for_sym;
  997.   char *demangled_name;
  998.  
  999.   btype = TYPE_VPTR_BASETYPE (dtype);
  1000.   check_stub_type (btype);
  1001.   if (btype != dtype)
  1002.     vtbl = value_cast (lookup_pointer_type (btype), arg);
  1003.   else
  1004.     vtbl = arg;
  1005.   vtbl = value_ind (value_field (value_ind (vtbl), TYPE_VPTR_FIELDNO (btype)));
  1006.  
  1007.   /* Check that VTBL looks like it points to a virtual function table.  */
  1008.   i = find_pc_misc_function (VALUE_ADDRESS (vtbl));
  1009.   if (i < 0 || ! VTBL_PREFIX_P (misc_function_vector[i].name))
  1010.     {
  1011.       /* If we expected to find a vtable, but did not, let the user
  1012.      know that we aren't happy, but don't throw an error.
  1013.      FIXME: there has to be a better way to do this.  */
  1014.       struct type *error_type = (struct type *)xmalloc (sizeof (struct type));
  1015.       bcopy (VALUE_TYPE (arg), error_type, sizeof (struct type));
  1016.       TYPE_NAME (error_type) = savestring ("suspicious *", sizeof ("suspicious *"));
  1017.       VALUE_TYPE (arg) = error_type;
  1018.       return arg;
  1019.     }
  1020.  
  1021.   /* Now search through the virtual function table.  */
  1022.   entry = value_ind (vtbl);
  1023.   nelems = longest_to_int (value_as_long (value_field (entry, 2)));
  1024.   for (i = 1; i <= nelems; i++)
  1025.     {
  1026.       entry = value_subscript (vtbl, value_from_longest (builtin_type_int, 
  1027.                               (LONGEST) i));
  1028.       offset = longest_to_int (value_as_long (value_field (entry, 0)));
  1029.       if (offset < best_offset)
  1030.     {
  1031.       best_offset = offset;
  1032.       best_entry = entry;
  1033.     }
  1034.     }
  1035.   if (best_entry == 0)
  1036.     return arg;
  1037.  
  1038.   /* Move the pointer according to BEST_ENTRY's offset, and figure
  1039.      out what type we should return as the new pointer.  */
  1040.   pc_for_sym = value_as_pointer (value_field (best_entry, 2));
  1041.   sym = find_pc_function (pc_for_sym);
  1042.   demangled_name = cplus_demangle (SYMBOL_NAME (sym), -1);
  1043.   *(strchr (demangled_name, ':')) = '\0';
  1044.   sym = lookup_symbol (demangled_name, 0, VAR_NAMESPACE, 0, 0);
  1045.   if (sym == 0)
  1046.     error ("could not find type declaration for `%s'", SYMBOL_NAME (sym));
  1047.   free (demangled_name);
  1048.   arg = value_add (value_cast (builtin_type_int, arg),
  1049.            value_field (best_entry, 0));
  1050.   VALUE_TYPE (arg) = lookup_pointer_type (SYMBOL_TYPE (sym));
  1051.   return arg;
  1052. }
  1053.  
  1054. /* ARG is a pointer object of type TYPE.  If TYPE has virtual
  1055.    function tables, probe ARG's tables (including the vtables
  1056.    of its baseclasses) to figure out the most derived type that ARG
  1057.    could actually be a pointer to.  */
  1058.  
  1059. value
  1060. value_from_vtable_info (arg, type)
  1061.      value arg;
  1062.      struct type *type;
  1063. {
  1064.   /* Take care of preliminaries.  */
  1065.   if (TYPE_VPTR_FIELDNO (type) < 0)
  1066.     fill_in_vptr_fieldno (type);
  1067.   if (TYPE_VPTR_FIELDNO (type) < 0 || VALUE_REPEATED (arg))
  1068.     return 0;
  1069.  
  1070.   return value_headof (arg, 0, type);
  1071. }
  1072.  
  1073. /* The value of a static class member does not depend
  1074.    on its instance, only on its type.  If FIELDNO >= 0,
  1075.    then fieldno is a valid field number and is used directly.
  1076.    Otherwise, FIELDNAME is the name of the field we are
  1077.    searching for.  If it is not a static field name, an
  1078.    error is signaled.  TYPE is the type in which we look for the
  1079.    static field member.
  1080.  
  1081.    Return zero if we couldn't find anything; the caller may signal
  1082.    an error in that case.  */
  1083.  
  1084. value
  1085. value_static_field (type, fieldname, fieldno)
  1086.      register struct type *type;
  1087.      char *fieldname;
  1088.      register int fieldno;
  1089. {
  1090.   register value v;
  1091.   struct symbol *sym;
  1092.   char *phys_name;
  1093.  
  1094.   if (fieldno < 0)
  1095.     {
  1096.       /* Look for static field.  */
  1097.       int i;
  1098.       for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
  1099.     if (! strcmp (TYPE_FIELD_NAME (type, i), fieldname))
  1100.       {
  1101.         if (TYPE_FIELD_STATIC (type, i))
  1102.           {
  1103.         fieldno = i;
  1104.         goto found;
  1105.           }
  1106.         else
  1107.           error ("field `%s' is not static", fieldname);
  1108.       }
  1109.       for (; i > 0; i--)
  1110.     {
  1111.       v = value_static_field (TYPE_BASECLASS (type, i), fieldname, -1);
  1112.       if (v != 0)
  1113.         return v;
  1114.     }
  1115.  
  1116.       if (destructor_name_p (fieldname, type))
  1117.     error ("Cannot get value of destructor");
  1118.  
  1119.       for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; i--)
  1120.     {
  1121.       if (! strcmp (TYPE_FN_FIELDLIST_NAME (type, i), fieldname))
  1122.         error ("Cannot get value of method \"%s\"", fieldname);
  1123.     }
  1124.       error("there is no field named %s", fieldname);
  1125.     }
  1126.  
  1127.  found:
  1128.   phys_name = TYPE_FIELD_STATIC_PHYSNAME (type, fieldno);
  1129.   sym = lookup_symbol (phys_name, 0, VAR_NAMESPACE, 0, NULL);
  1130.   if (! sym) error ("Internal error: could not find physical static variable named %s", phys_name);
  1131.  
  1132.   type = TYPE_FIELD_TYPE (type, fieldno);
  1133.   v = value_at (type, (CORE_ADDR)SYMBOL_BLOCK_VALUE (sym));
  1134.   return v;
  1135. }
  1136.  
  1137. /* Compute the address of the baseclass which is
  1138.    the INDEXth baseclass of TYPE.  The TYPE base
  1139.    of the object is at VALADDR.
  1140.  
  1141.    If ERRP is non-NULL, set *ERRP to be the errno code of any error,
  1142.    or 0 if no error.  In that case the return value is not the address
  1143.    of the baseclasss, but the address which could not be read
  1144.    successfully.  */
  1145.  
  1146. char *
  1147. baseclass_addr (type, index, valaddr, valuep, errp)
  1148.      struct type *type;
  1149.      int index;
  1150.      char *valaddr;
  1151.      value *valuep;
  1152.      int *errp;
  1153. {
  1154.   struct type *basetype = TYPE_BASECLASS (type, index);
  1155.  
  1156.   if (errp)
  1157.     *errp = 0;
  1158.  
  1159.   if (BASETYPE_VIA_VIRTUAL (type, index))
  1160.     {
  1161.       /* Must hunt for the pointer to this virtual baseclass.  */
  1162.       register int i, len = TYPE_NFIELDS (type);
  1163.       register int n_baseclasses = TYPE_N_BASECLASSES (type);
  1164.       char *vbase_name, *type_name = type_name_no_tag (basetype);
  1165.  
  1166.       if (TYPE_MAIN_VARIANT (basetype))
  1167.     basetype = TYPE_MAIN_VARIANT (basetype);
  1168.  
  1169.       vbase_name = (char *)alloca (strlen (type_name) + 8);
  1170.       sprintf (vbase_name, "_vb$%s", type_name);
  1171.       /* First look for the virtual baseclass pointer
  1172.      in the fields.  */
  1173.       for (i = n_baseclasses; i < len; i++)
  1174.     {
  1175.       if (! strcmp (vbase_name, TYPE_FIELD_NAME (type, i)))
  1176.         {
  1177.           value val = allocate_value (basetype);
  1178.           CORE_ADDR addr;
  1179.           int status;
  1180.  
  1181.           addr
  1182.         = unpack_pointer (TYPE_FIELD_TYPE (type, i),
  1183.                   valaddr + (TYPE_FIELD_BITPOS (type, i) / 8));
  1184.  
  1185.           status = target_read_memory (addr,
  1186.                        VALUE_CONTENTS_RAW (val),
  1187.                        TYPE_LENGTH (basetype));
  1188.           VALUE_LVAL (val) = lval_memory;
  1189.           VALUE_ADDRESS (val) = addr;
  1190.  
  1191.           if (status != 0)
  1192.         {
  1193.           if (valuep)
  1194.             *valuep = NULL;
  1195.           release_value (val);
  1196.           value_free (val);
  1197.           if (errp)
  1198.             *errp = status;
  1199.           return (char *)addr;
  1200.         }
  1201.           else
  1202.         {
  1203.           if (valuep)
  1204.             *valuep = val;
  1205.           return (char *) VALUE_CONTENTS (val);
  1206.         }
  1207.         }
  1208.     }
  1209.       /* Not in the fields, so try looking through the baseclasses.  */
  1210.       for (i = index+1; i < n_baseclasses; i++)
  1211.     {
  1212.       char *baddr;
  1213.  
  1214.       baddr = baseclass_addr (type, i, valaddr, valuep, errp);
  1215.       if (baddr)
  1216.         return baddr;
  1217.     }
  1218.       /* Not found.  */
  1219.       if (valuep)
  1220.     *valuep = 0;
  1221.       return 0;
  1222.     }
  1223.  
  1224.   /* Baseclass is easily computed.  */
  1225.   if (valuep)
  1226.     *valuep = 0;
  1227.   return valaddr + TYPE_BASECLASS_BITPOS (type, index) / 8;
  1228. }
  1229.  
  1230. /* Ugly hack to convert method stubs into method types.
  1231.  
  1232.    He ain't kiddin'.  This demangles the name of the method into a string
  1233.    including argument types, parses out each argument type, generates
  1234.    a string casting a zero to that type, evaluates the string, and stuffs
  1235.    the resulting type into an argtype vector!!!  Then it knows the type
  1236.    of the whole function (including argument types for overloading),
  1237.    which info used to be in the stab's but was removed to hack back
  1238.    the space required for them.  */
  1239. void
  1240. check_stub_method (type, i, j)
  1241.      struct type *type;
  1242.      int i, j;
  1243. {
  1244.   extern char *gdb_mangle_typename (), *strchr ();
  1245.   struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
  1246.   char *field_name = TYPE_FN_FIELDLIST_NAME (type, i);
  1247.   char *inner_name = gdb_mangle_typename (type);
  1248.   int mangled_name_len = (strlen (field_name)
  1249.               + strlen (inner_name)
  1250.               + strlen (TYPE_FN_FIELD_PHYSNAME (f, j))
  1251.               + 1);
  1252.   char *mangled_name;
  1253.   char *demangled_name;
  1254.   char *argtypetext, *p;
  1255.   int depth = 0, argcount = 1;
  1256.   struct type **argtypes;
  1257.  
  1258.   if (OPNAME_PREFIX_P (field_name))
  1259.     {
  1260.       char *opname = cplus_mangle_opname (field_name + 3);
  1261.       if (opname == NULL)
  1262.     error ("No mangling for \"%s\"", field_name);
  1263.       mangled_name_len += strlen (opname);
  1264.       mangled_name = (char *)xmalloc (mangled_name_len);
  1265.  
  1266.       strncpy (mangled_name, field_name, 3);
  1267.       mangled_name[3] = '\0';
  1268.       strcat (mangled_name, opname);
  1269.     }
  1270.   else
  1271.     {
  1272.       mangled_name = (char *)xmalloc (mangled_name_len);
  1273.       strcpy (mangled_name, TYPE_FN_FIELDLIST_NAME (type, i));
  1274.     }
  1275.   strcat (mangled_name, inner_name);
  1276.   strcat (mangled_name, TYPE_FN_FIELD_PHYSNAME (f, j));
  1277.   demangled_name = cplus_demangle (mangled_name, 0);
  1278.  
  1279.   /* Now, read in the parameters that define this type.  */
  1280.   argtypetext = strchr (demangled_name, '(') + 1;
  1281.   p = argtypetext;
  1282.   while (*p)
  1283.     {
  1284.       if (*p == '(')
  1285.     depth += 1;
  1286.       else if (*p == ')')
  1287.     depth -= 1;
  1288.       else if (*p == ',' && depth == 0)
  1289.     argcount += 1;
  1290.  
  1291.       p += 1;
  1292.     }
  1293.   /* We need one more slot for the void [...] or NULL [end of arglist] */
  1294.   argtypes = (struct type **)xmalloc ((argcount+1) * sizeof (struct type *));
  1295.   p = argtypetext;
  1296.   argtypes[0] = lookup_pointer_type (type);
  1297.   argcount = 1;
  1298.  
  1299.   if (*p != ')')            /* () means no args, skip while */
  1300.     {
  1301.       while (*p)
  1302.     {
  1303.       if (*p == '(')
  1304.         depth += 1;
  1305.       else if (*p == ')')
  1306.         depth -= 1;
  1307.  
  1308.       if (depth <= 0 && (*p == ',' || *p == ')'))
  1309.         {
  1310.           char *tmp = (char *)alloca (p - argtypetext + 4);
  1311.           value val;
  1312.           tmp[0] = '(';
  1313.           bcopy (argtypetext, tmp+1, p - argtypetext);
  1314.           tmp[p-argtypetext+1] = ')';
  1315.           tmp[p-argtypetext+2] = '0';
  1316.           tmp[p-argtypetext+3] = '\0';
  1317.           val = parse_and_eval (tmp);
  1318.           argtypes[argcount] = VALUE_TYPE (val);
  1319.           argcount += 1;
  1320.           argtypetext = p + 1;
  1321.         }
  1322.       p += 1;
  1323.     }
  1324.     }
  1325.  
  1326.   if (p[-2] != '.')            /* ... */
  1327.     argtypes[argcount] = builtin_type_void;    /* Ellist terminator */
  1328.   else
  1329.     argtypes[argcount] = NULL;        /* List terminator */
  1330.  
  1331.   free (demangled_name);
  1332.  
  1333.   type = lookup_method_type (type, TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f, j)), argtypes);
  1334.   /* Free the stub type...it's no longer needed.  */
  1335.   free (TYPE_FN_FIELD_TYPE (f, j));
  1336.   TYPE_FN_FIELD_PHYSNAME (f, j) = mangled_name;
  1337.   TYPE_FN_FIELD_TYPE (f, j) = type;
  1338. }
  1339.  
  1340. long
  1341. unpack_field_as_long (type, valaddr, fieldno)
  1342.      struct type *type;
  1343.      char *valaddr;
  1344.      int fieldno;
  1345. {
  1346.   long val;
  1347.   int bitpos = TYPE_FIELD_BITPOS (type, fieldno);
  1348.   int bitsize = TYPE_FIELD_BITSIZE (type, fieldno);
  1349.  
  1350.   bcopy (valaddr + bitpos / 8, &val, sizeof val);
  1351.   SWAP_TARGET_AND_HOST (&val, sizeof val);
  1352.  
  1353.   /* Extracting bits depends on endianness of the machine.  */
  1354. #if BITS_BIG_ENDIAN
  1355.   val = val >> (sizeof val * 8 - bitpos % 8 - bitsize);
  1356. #else
  1357.   val = val >> (bitpos % 8);
  1358. #endif
  1359.  
  1360.   if (bitsize < 8 * sizeof (val))
  1361.     val &= (((unsigned long)1) << bitsize) - 1;
  1362.   return val;
  1363. }
  1364.  
  1365. /* Modify the value of a bitfield.  ADDR points to a block of memory in
  1366.    target byte order; the bitfield starts in the byte pointed to.  FIELDVAL
  1367.    is the desired value of the field, in host byte order.  BITPOS and BITSIZE
  1368.    indicate which bits (in target bit order) comprise the bitfield.  */
  1369.  
  1370. void
  1371. modify_field (addr, fieldval, bitpos, bitsize)
  1372.      char *addr;
  1373.      int fieldval;
  1374.      int bitpos, bitsize;
  1375. {
  1376.   long oword;
  1377.  
  1378.   /* Reject values too big to fit in the field in question,
  1379.      otherwise adjoining fields may be corrupted.  */
  1380.   if (bitsize < (8 * sizeof (fieldval))
  1381.       && 0 != (fieldval & ~((1<<bitsize)-1)))
  1382.     error ("Value %d does not fit in %d bits.", fieldval, bitsize);
  1383.   
  1384.   bcopy (addr, &oword, sizeof oword);
  1385.   SWAP_TARGET_AND_HOST (&oword, sizeof oword);        /* To host format */
  1386.  
  1387.   /* Shifting for bit field depends on endianness of the target machine.  */
  1388. #if BITS_BIG_ENDIAN
  1389.   bitpos = sizeof (oword) * 8 - bitpos - bitsize;
  1390. #endif
  1391.  
  1392.   /* Mask out old value, while avoiding shifts >= longword size */
  1393.   if (bitsize < 8 * sizeof (oword))
  1394.     oword &= ~(((((unsigned long)1) << bitsize) - 1) << bitpos);
  1395.   else
  1396.     oword &= ~((-1) << bitpos);
  1397.   oword |= fieldval << bitpos;
  1398.  
  1399.   SWAP_TARGET_AND_HOST (&oword, sizeof oword);        /* To target format */
  1400.   bcopy (&oword, addr, sizeof oword);
  1401. }
  1402.  
  1403. /* Convert C numbers into newly allocated values */
  1404.  
  1405. value
  1406. value_from_longest (type, num)
  1407.      struct type *type;
  1408.      register LONGEST num;
  1409. {
  1410.   register value val = allocate_value (type);
  1411.   register enum type_code code = TYPE_CODE (type);
  1412.   register int len = TYPE_LENGTH (type);
  1413.  
  1414.   /* FIXME, we assume that pointers have the same form and byte order as
  1415.      integers, and that all pointers have the same form.  */
  1416.   if (code == TYPE_CODE_INT  || code == TYPE_CODE_ENUM || 
  1417.       code == TYPE_CODE_CHAR || code == TYPE_CODE_PTR)
  1418.     {
  1419.       if (len == sizeof (char))
  1420.     * (char *) VALUE_CONTENTS_RAW (val) = num;
  1421.       else if (len == sizeof (short))
  1422.     * (short *) VALUE_CONTENTS_RAW (val) = num;
  1423.       else if (len == sizeof (int))
  1424.     * (int *) VALUE_CONTENTS_RAW (val) = num;
  1425.       else if (len == sizeof (long))
  1426.     * (long *) VALUE_CONTENTS_RAW (val) = num;
  1427. #ifdef LONG_LONG
  1428.       else if (len == sizeof (long long))
  1429.     * (long long *) VALUE_CONTENTS_RAW (val) = num;
  1430. #endif
  1431.       else
  1432.     error ("Integer type encountered with unexpected data length.");
  1433.     }
  1434.   else
  1435.     error ("Unexpected type encountered for integer constant.");
  1436.  
  1437.   /* num was in host byte order.  So now put the value's contents
  1438.      into target byte order.  */
  1439.   SWAP_TARGET_AND_HOST (VALUE_CONTENTS_RAW (val), len);
  1440.  
  1441.   return val;
  1442. }
  1443.  
  1444. value
  1445. value_from_double (type, num)
  1446.      struct type *type;
  1447.      double num;
  1448. {
  1449.   register value val = allocate_value (type);
  1450.   register enum type_code code = TYPE_CODE (type);
  1451.   register int len = TYPE_LENGTH (type);
  1452.  
  1453.   if (code == TYPE_CODE_FLT)
  1454.     {
  1455.       if (len == sizeof (float))
  1456.     * (float *) VALUE_CONTENTS_RAW (val) = num;
  1457.       else if (len == sizeof (double))
  1458.     * (double *) VALUE_CONTENTS_RAW (val) = num;
  1459.       else
  1460.     error ("Floating type encountered with unexpected data length.");
  1461.     }
  1462.   else
  1463.     error ("Unexpected type encountered for floating constant.");
  1464.  
  1465.   /* num was in host byte order.  So now put the value's contents
  1466.      into target byte order.  */
  1467.   SWAP_TARGET_AND_HOST (VALUE_CONTENTS_RAW (val), len);
  1468.  
  1469.   return val;
  1470. }
  1471.  
  1472. /* Deal with the value that is "about to be returned".  */
  1473.  
  1474. /* Return the value that a function returning now
  1475.    would be returning to its caller, assuming its type is VALTYPE.
  1476.    RETBUF is where we look for what ought to be the contents
  1477.    of the registers (in raw form).  This is because it is often
  1478.    desirable to restore old values to those registers
  1479.    after saving the contents of interest, and then call
  1480.    this function using the saved values.
  1481.    struct_return is non-zero when the function in question is
  1482.    using the structure return conventions on the machine in question;
  1483.    0 when it is using the value returning conventions (this often
  1484.    means returning pointer to where structure is vs. returning value). */
  1485.  
  1486. value
  1487. value_being_returned (valtype, retbuf, struct_return)
  1488.      register struct type *valtype;
  1489.      char retbuf[REGISTER_BYTES];
  1490.      int struct_return;
  1491.      /*ARGSUSED*/
  1492. {
  1493.   register value val;
  1494.   CORE_ADDR addr;
  1495.  
  1496. #if defined (EXTRACT_STRUCT_VALUE_ADDRESS)
  1497.   /* If this is not defined, just use EXTRACT_RETURN_VALUE instead.  */
  1498.   if (struct_return) {
  1499.     addr = EXTRACT_STRUCT_VALUE_ADDRESS (retbuf);
  1500.     if (!addr)
  1501.       error ("Function return value unknown");
  1502.     return value_at (valtype, addr);
  1503.   }
  1504. #endif
  1505.  
  1506.   val = allocate_value (valtype);
  1507.   EXTRACT_RETURN_VALUE (valtype, retbuf, VALUE_CONTENTS_RAW (val));
  1508.  
  1509.   return val;
  1510. }
  1511.  
  1512. /* Should we use EXTRACT_STRUCT_VALUE_ADDRESS instead of
  1513.    EXTRACT_RETURN_VALUE?  GCC_P is true if compiled with gcc
  1514.    and TYPE is the type (which is known to be struct, union or array).
  1515.  
  1516.    On most machines, the struct convention is used unless we are
  1517.    using gcc and the type is of a special size.  */
  1518. #if !defined (USE_STRUCT_CONVENTION)
  1519. #define USE_STRUCT_CONVENTION(gcc_p, type)\
  1520.   (!((gcc_p) && (TYPE_LENGTH (value_type) == 1                \
  1521.          || TYPE_LENGTH (value_type) == 2             \
  1522.              || TYPE_LENGTH (value_type) == 4             \
  1523.          || TYPE_LENGTH (value_type) == 8             \
  1524.          )                                            \
  1525.      ))
  1526. #endif
  1527.  
  1528. /* Return true if the function specified is using the structure returning
  1529.    convention on this machine to return arguments, or 0 if it is using
  1530.    the value returning convention.  FUNCTION is the value representing
  1531.    the function, FUNCADDR is the address of the function, and VALUE_TYPE
  1532.    is the type returned by the function.  GCC_P is nonzero if compiled
  1533.    with GCC.  */
  1534.  
  1535. int
  1536. using_struct_return (function, funcaddr, value_type, gcc_p)
  1537.      value function;
  1538.      CORE_ADDR funcaddr;
  1539.      struct type *value_type;
  1540.      int gcc_p;
  1541.      /*ARGSUSED*/
  1542. {
  1543.   register enum type_code code = TYPE_CODE (value_type);
  1544.  
  1545.   if (code == TYPE_CODE_ERROR)
  1546.     error ("Function return type unknown.");
  1547.  
  1548.   if (code == TYPE_CODE_STRUCT ||
  1549.       code == TYPE_CODE_UNION ||
  1550.       code == TYPE_CODE_ARRAY)
  1551.     return USE_STRUCT_CONVENTION (gcc_p, value_type);
  1552.  
  1553.   return 0;
  1554. }
  1555.  
  1556. /* Store VAL so it will be returned if a function returns now.
  1557.    Does not verify that VAL's type matches what the current
  1558.    function wants to return.  */
  1559.  
  1560. void
  1561. set_return_value (val)
  1562.      value val;
  1563. {
  1564.   register enum type_code code = TYPE_CODE (VALUE_TYPE (val));
  1565.   double dbuf;
  1566.   LONGEST lbuf;
  1567.  
  1568.   if (code == TYPE_CODE_ERROR)
  1569.     error ("Function return type unknown.");
  1570.  
  1571.   if (code == TYPE_CODE_STRUCT
  1572.       || code == TYPE_CODE_UNION)
  1573.     error ("Specifying a struct or union return value is not supported.");
  1574.  
  1575.   /* FIXME, this is bogus.  We don't know what the return conventions
  1576.      are, or how values should be promoted.... */
  1577.   if (code == TYPE_CODE_FLT)
  1578.     {
  1579.       dbuf = value_as_double (val);
  1580.  
  1581.       STORE_RETURN_VALUE (VALUE_TYPE (val), (char *)&dbuf);
  1582.     }
  1583.   else
  1584.     {
  1585.       lbuf = value_as_long (val);
  1586.       STORE_RETURN_VALUE (VALUE_TYPE (val), (char *)&lbuf);
  1587.     }
  1588. }
  1589.  
  1590. void
  1591. _initialize_values ()
  1592. {
  1593.   add_cmd ("convenience", no_class, show_convenience,
  1594.         "Debugger convenience (\"$foo\") variables.\n\
  1595. These variables are created when you assign them values;\n\
  1596. thus, \"print $foo=1\" gives \"$foo\" the value 1.  Values may be any type.\n\n\
  1597. A few convenience variables are given values automatically:\n\
  1598. \"$_\"holds the last address examined with \"x\" or \"info lines\",\n\
  1599. \"$__\" holds the contents of the last address examined with \"x\".",
  1600.        &showlist);
  1601.  
  1602.   add_cmd ("values", no_class, show_values,
  1603.        "Elements of value history around item number IDX (or last ten).",
  1604.        &showlist);
  1605. }
  1606.